package com.lwt.hospital.controller;

import com.lwt.hospital.model.Appointment;
import com.lwt.hospital.model.Question;
import com.lwt.hospital.model.User;
import com.lwt.hospital.service.Demo;
import com.lwt.hospital.service.UserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 丁兆顺
 * @date 2020/5/18 0:20
 * @desciption
 */
@RestController
public class UserController extends BaseController {

    @Resource
    private Demo demo;

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(@RequestBody User user, HttpSession session){
        System.out.println(user);
        if(StringUtils.isEmpty(user.getAccount() )&& StringUtils.isEmpty(user.getPassword())
                && StringUtils.isEmpty(user.getAccount()) && user.getIdentity() != 1 && user.getIdentity() != 2 && user.getIdentity() != 3){
            return getCommonERROR("数据缺失，请检查是否填写密码和账号");
        }else{
            if(demo.login(user,session)){
                // 应该返回一个用户数据
                return getCommonSUCESS("登录成功");
            }else{
                return getCommonERROR("账号或密码错误，请检查后重试");
            }
        }
    }

    @RequestMapping("/register")
    public String register(@RequestBody User user,HttpSession session){
        System.out.println(user);

        String result = userService.userRegister(user, session);
        System.out.println(result);
        if("注册成功！".equals(result)){
            return getCommonSUCESS(result);
        }else{
            return getCommonERROR(result);
        }
    }


    // 这个接口也用来返回用户信息算了
    @RequestMapping("/ifLogin")
    public String ifLogin(HttpSession session){
        if(userService.userIfLogin(session)){
            return getCommonSUCESS(session.getAttribute("userInfo"));
//            User user = userService.getUserInfo(userInfo.getAccount());
        }else{
            return getCommonERROR("用户未登录");
        }
    }

    /**
     *
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/modifyUserInfo")
    public String ifLogin(@RequestBody User user,HttpSession session){
        if(user != null){
            Integer result = userService.modifyUserInfo(user, session);
            if(result == -1){
                return getCommonERROR("该手机号已被注册！");
            }else if(result == 1){
                return getCommonSUCESS("修改成功");
            }else{
            return getCommonERROR("修改失败！");
            }
        }else{
            return getCommonERROR("请求失败！");
        }
    }

    @RequestMapping("/getUserAppointment")
    public String getUserAppointment(HttpSession session){
        return getCommonSUCESS(userService.selectAppointment(session));
    }

    @RequestMapping("/addNewQuestion")
    public String addNewQuestion(@RequestBody Question question,HttpSession session){
        System.out.println(question);
        Integer integer = userService.addNewQuestion(question, session);
        if(integer > 0){
            return getCommonSUCESS("提交成功");
        }else{
            return getCommonERROR("提交失败");
        }
    }

    @RequestMapping("/addAppointment")
    public String addAppointment(@RequestBody Appointment appointment,HttpSession session){
        System.out.println(appointment);
        Integer integer = userService.addAppointment(appointment, session);
        if(integer > 0){
            return getCommonSUCESS("预约成功");
        }else{
            return getCommonERROR("预约失败");
        }
    }

    @RequestMapping("/getOldAppointment")
    public String getOldAppointment(HttpSession session){
        return getCommonSUCESS(userService.getOldAppointment(session));
    }

    @RequestMapping("/getOldQuestion")
    public String getOldQuestion(HttpSession session){
        return getCommonSUCESS(userService.getOldQuestion(session));
    }


    @RequestMapping("/consultationHistory")
    public String consultationHistory(@RequestBody Appointment appointment,HttpSession session){
            return null;
    }

    @RequestMapping("/selectUsers")
    public String selectUser(){
        return getCommonSUCESS(userService.selectUsers());
    }

    @RequestMapping("/delUser")
    public String delUser(@RequestBody User user){
        int result = userService.delUser(user.getId());
        if(result > 0){
            return getCommonSUCESS("操作成功！");
        }else{
            return getCommonERROR("操作失败!");
        }
    }
}
